def init_test_offer(engine):
    from mall.models.member import Member
    from mall.models.product import Product
    from mall.models.offer import Offer
    conn = engine.connect()

    conn.execute(Offer.__table__.delete())
    from sqlalchemy.sql import select
    result = conn.execute(select([Product.__table__.c.id]))

    products = [p[0] for p in result]

    result = conn.execute(select([Member]))

    members = [m[0] for m in result]

    i = 0
    import random

    for p in products:
        for m in members:
            i += 1
            conn.execute(Offer.__table__.insert(),
                         id=i,
                         product_id=p,
                         dealer_id=m,
                         count=random.randint(10, 1000),
                         slug='cdk',
                         price=random.randint(2, 30)
                         )
    # session.query(Offer).delete()
    # product_list = session.query(Product).all()
    #
    # member_list = session.query(Member).all()
    # import random
    # i = 0
    # for p in product_list:
    #
    #     for m in member_list:
    #         i += 1
    #         offer = Offer(
    #             id=i,
    #             product_id=p.id,
    #             dealer_id=m.id,
    #             count=random.randint(10, 100),
    #             slug='cdk',
    #             price=random.randint(2, 200),
    #
    #         )
    #         session.add(offer)
    # session.commit()
    offer_table = Offer.__table__
    from sqlalchemy.sql import  select
    for p in  products:
        product_offer_list = conn.execute(select([Offer]).where(offer_table.c.product_id == p))

        offer_id = random.choice([offer[0] for offer in product_offer_list])

        upd_stmt = offer_table.update().values(mask='R').where(offer_table.c.id == offer_id)
        conn.execute(upd_stmt)
